//
//  BZYSearchTableViewController.m
//  BZYMeituan
//
//  Created by 毕仲阳 on 16/9/17.
//  Copyright © 2016年 mycompany. All rights reserved.
//

#import "BZYSearchTableViewController.h"
#import "BZYCityModel.h"
@interface BZYSearchTableViewController ()
//总的城市数据数组
@property (nonatomic, strong) NSArray *citiesArray;

//搜索的结果数组
@property (nonatomic, strong) NSMutableArray *searchResultArray;

@end

@implementation BZYSearchTableViewController

- (void)setSearchText:(NSString *)searchText
{
    _searchText = [searchText copy];
    
    _searchText = _searchText.lowercaseString;
    //清空搜索结果的数组，以免数组元素越增越多
    [self.searchResultArray removeAllObjects];
    
    for (BZYCityModel *cityModel in self.citiesArray) {
        if ([cityModel.name containsString:_searchText] || [cityModel.pinYin containsString:_searchText] || [cityModel.pinYinHead containsString:_searchText])
        {
            [self.searchResultArray addObject:cityModel.name];
        }
    }
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取总的城市数据
    NSArray *citiesPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]  pathForResource:@"cities.plist" ofType:nil]];
    
    self.citiesArray = [NSArray yy_modelArrayWithClass:NSClassFromString(@"BZYCityModel") json:citiesPlist];
    
    // 可变数据的加载
    self.searchResultArray = [NSMutableArray array];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchResultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    cell.textLabel.text = self.searchResultArray[indexPath.row];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"共有%zd个搜索结果",self.searchResultArray.count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSNotificationCenter defaultCenter] postNotificationName:BZYCityDidChangeNotification object:nil userInfo:@{BZYCityNameKey : self.searchResultArray[indexPath.row]}];
    [self dismissViewControllerAnimated:true completion:nil];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
